If you want to start using the data store to save data in between games on the home tab, select that.
Go to this game settings, click the cog.
If you haven't saved the game, it'll tell you that you need to do that.
But we've already saved our game at least one time.
Let's go to security and then enable studio access to API services.
The data store is one of those services we need to enable this and then hit save.
Let's start by saving off our leaderboard data to the data store.
So we have our points, our kills and our deaths in between games.
I'm going to go to server script service here and Explorer.
Open that up.
Open the game manager if you don't have it open already.
I'm going to go to the top of the game manager and I'm going to define two variables.
Local DSS for data Store Service Game Get service data store service.
I'm going to get another one called DS Data store.
Got that from data store service.
Get data store my data.
Cool.
So we got my data right here.
That's going to be the name of the data store.
This is going to be the variable to access the data store.
Let's go down to above where we get our player added event and connect it to a function.
Let's make another function local function.
I'll call this initialize player data and we'll pass a player in.
Then let's make a variable called data.
We'll just initialize it to nil.
For now I'm going to do something a little different.
I do local, but I'm going to actually initialize two variables here.
I'm going to have success and a message and we're going to initialize this by doing a protected call
with an anonymous function.
Nothing's going to get passed into that function.
We're going to get our data back by doing a DS, get async.
That's an asynchronous call to the data store and we'll use player user ID as the key.
So we're going to go to our data store, we're going to get the data for the user, we're going to key
off its ID and then it's going to come back.
If we save data off, why put this in a P call?
Well, this is an asynchronous call.
It's an HTTP call.
We might have some problems.
If we do, we're going to get a failure and then we're going to get a reason for the failure.
So it's a little bit of extra overhead, but you don't want to mess this up and then people have a whole
bunch of data saved off and then they rage because there's a there's a mess up.
So now what we need to do is check to see if the call was not successful.
So I'll say, if not success, then player kick the player because it's better to kick him out and have
him get back in than to write bad data to to his character.
Because then if he leaves, we're going to save off bad data.
All of his stuff is ruined.
So kick them data store on reachable.
Please try again.
So give him give him some information and then put that message there.
All right.
That's going to give a little dialogue to pops up.
He gets kicked or she gets kicked, either one.
And then you look up and you're like, Oh, what's going on?
Oh, a data store is unreachable.
A good thing I didn't join the game and ruined my data.
So now we get down here because we haven't been kicked.
Say if data oh, we got data will say player leader stats, we got points and there's a value associated
with that.
So dot value that will be in the data points.
Yeah, there we go.
And we got kills and deaths.
Let's save this off.
That way.
I'll speed things up a little bit.
We're going to change this to kills and then data will have kills.
It doesn't yet.
So you'll be like, how does data have kills?
Well, we don't know that yet, but I'm going to show you in a minute.
And then deaths that I smell, deaths, right?
Yes.
What matters is if you spell it the same as up here, Right?
Because that's your leaderboard data.
All right.
So that we had already saved data off.
We successfully got the data back, but it might be your first time playing.
There might not be data.
So what we're going to do is we're going to say else data equals points equals zero, or make a little
table.
A key value pair table kills equals zero, and then deaths equals zero.
So now table will have points, kills and deaths.
They'll all equal zero, but it will have those those keys in there for our key value pair.
All right, We get down to here, we get down to the bottom, we're going to say data store set async.
So we'll do a save.
When people enter the game, we're going to get the data and we'll just do a save here using the player's
user ID and the data that we got.
That's why it's really important or one of the reasons why it's really important to kick the player.
If the player is bad, if the data is bad, because if the data comes in at zero or not, correct,
we override his data, we save it off.
Everything that would have been in here is gone.
So that's why we use protected calls.
I didn't use the protected call here because we're going to do periodic calls to the data store anyway.
And if it doesn't save right away, we are going to be able to save it.
This area in here is going to be called for first time players.
So one other thing I'm going to do is put a note saying something like, maybe we could fire off a welcome
screen, right?
Maybe do a remote event, maybe do a remote event to a welcome screen.
And we could print something to saying first time player, first time player, and we'll we'll remove
that after we test this out and are sure that everything's going to work.
Now, where does this get called?
We have this initialized player data.
Let's copy that.
Let's take a look here right where the player is added.
But before the character added and the character appearance loaded, we have to make sure there's a
board on the player.
So go below there because we're going to use that board to initialize the player data.
I'm going to do a control V, I'm going to put initialized player data right here.
All right, so let's try this out.
Keep in mind, we can't really save anything off.
We do an initial save here, but we can determine if it's the first time playing or not because there'll
be no data store.
Then I'm going to induce an error to see if our kick actually works.
Let's hit our play button.
Oh.
Before we do that, let's go to view hit the output window so we could see any errors.
Now I'm going to go hit my play button, this little tiny one right here.
And we should see first time player message.
There it is.
First time player.
Let's go ahead and turn the game off.
And then we're going to play it again and we should not see first time player.
And we don't.
That's cool.
We don't see the first time player.
Let's induce an error to make sure our P call kicks us if we have a problem.
Let's go to home Game settings Security.
I'm going to turn off enable API access or enable studio access to API services.
That way we're going to have a problem.
Let's go ahead and play this.
Boom.
Look at that.
This connected.
You were kicked from this experience data store unreachable.
Please try again.
And then we got a little message down here.
Perfect.
That's what we want.
We want to make sure that people can't get in and mess up their data if it's not right.
Let's go back to our home game Settings Security Enable studio access to API services.
And we're good.
Now I want to show you one more thing.
What happens if you save data off?
But now the data is in the data store and you want to do first time entry stuff.
How do you clear the data store?
Well, you have to send a set, a sync for the user data, and I'm just doing this for each user.
As false.
This is going to clear my data.
And this is the only place I'm saving it.
So I'm going to enter the game.
I'm going to have data, I'm going to leave the game.
This is going to be the last time we talked to the data store.
We're setting our user ID data to false.
That's essentially clearing it out.
Let's take a look at our view tab, though.
We're not going to see anything because it wasn't the first time player.
Now let's exit.
Let's go ahead and change this back because we cleared the data store.
We want it to work regular now.
But now it should be the first time playing because for this player, because we cleared his data.
First time player.
There we go.
So that's how you clear the data for the data store for individual players.
Anyway, if you want to scrap the entire data store, you may want to just go ahead and make another
data store right here, maybe version two or something.